<template>
  <j-dialog v-model="showAccount" title="审核收款" :z-index="9" :width="700" draggable>
    <div class="mb20">
      <t-alert :closable="false" type="success" :text="receiptList[0].buName" />
    </div>
    <div class="mb10">
      <JPicker size="default" v-model="billDate" />
    </div>
    <div class="flex">
      <div style="border: grey;padding: 20px;border-radius: 4px;">
        <div>
          <j-space>
            <span>账号</span>
            <span><JAuto style="width: 200px;" cat="accountList" v-model="accName" @change="changeAcc" /></span>
          </j-space>
        </div>
        <div class="mt20">
          <j-space>
            <span>金额</span>
            <span><JInput style="width: 200px;" v-model="accAmount" money /></span>
          </j-space>
        </div>
      </div>
      <div style="border: grey;margin-left: 50px;padding: 20px;border-radius: 4px;">
        <div>
          <j-space>
            <span>账号1</span>
            <span><JAuto style="width: 200px;" cat="accountList" v-model="accName1" @change="changeAcc1" /></span>
          </j-space>
        </div>
        <div class="mt20">
          <j-space>
            <span>金额1</span>
            <span><JInput style="width: 200px;" v-model="accAmount1" money /></span>
          </j-space>
        </div>
      </div>
    </div>
    <div style="margin-top: 10px;">
      <tiny-card shadow="never">
        <div>
          <j-space>
            <div>应收合计</div>
            <div class="price">{{ys}}</div>
          </j-space>
        </div>
        <div class="mt20">
          <j-space>
            <div>收款优惠</div>
            <div class="price yh">{{yhAmount}}</div>
          </j-space>
        </div>
        <div class="mt20">
          <j-space>
            <div>本次收款</div>
            <div class="price">{{nowCheck}}</div>
          </j-space>
        </div>
      </tiny-card>
    </div>
    <div class="tc" style="margin-top: 50px">
      <JButton @click="setAccount" style="width: 150px" type="info" size="large">审核通过</JButton>
    </div>
  </j-dialog>
</template>
<script >
// 弹出审核
import tool from "@/utils/tool";

export default {
  data () {
    return {
      showAccount: false,
      accName: '',
      billDate: tool.getDay(),
      accName1:'',
      accId: null,
      accId1: null,
      accAmount: null,
      accAmount1: null,
      receiptList: [],
      row: {},
      ys: null,
      nowCheck: null,
      yhAmount: null,
    }
  },
  emits: ['saveSuc'],
  methods: {
    openDia (row, detailList) {
      let gp = tool.xe.groupBy(detailList, 'buId')
      this.receiptList = gp[row.buId]
      if (row.transTypeName === '小计') {
        if(row.accounts && row.accounts.length > 0) {
          this.accId = row.accounts[0].accId
          this.accName = row.accounts[0].accName
          this.accAmount = row.accounts[0].accAmount
          if(row.accounts.length === 2) {
            this.accId1 = row.accounts[1].accId
            this.accName1 = row.accounts[1].accName
            this.accAmount1 = row.accounts[1].accAmount
          }
        }
        else {
          this.accAmount = null
          this.accId = null
          this.accName = null
          this.accAmount1 = null
          this.accId1 = null
          this.accName1 = null
        }
        this.currentRow = row
        this.showAccount = true;
      }
      this.row = tool.xe.clone(row, true)
      tool.post('receipt/lastAccount', {buId: this.receiptList[0].buId}).then(ret => {
        if(ret.stat) {
          this.accId = ret.data.accId
          this.accName = ret.data.accName
        }
      })
      this.accAmount = tool.xe.toFixed(tool.xe.sum(this.receiptList, 'nowCheck'),2)
      this.nowCheck = tool.xe.toFixed(tool.xe.sum(this.receiptList, 'nowCheck'),2)
      this.ys = tool.xe.toFixed(tool.xe.sum(this.receiptList, 'leftAmount'),2)
      this.yhAmount = tool.xe.toFixed(tool.xe.sum(this.receiptList, 'yhAmount'),2)
      this.showAccount = true
    },
    changeAcc (ret) {
      this.accId = ret?ret.id:''
    },
    changeAcc1 (ret) {
      this.accId1 = ret?ret.id:''
    },
    setAccount () {
      if(!this.billDate) {
        return tool.warning('请选择时间')
      }
      if(tool.dayjs(this.billDate).isAfter(tool.getDay())) {
        return tool.warning('时间不能超过今天')
      }
      this.accountInfo = []
      if(this.accId) {
        this.accAmount = tool.xe.toNumber(this.accAmount)
        if(this.accAmount) {
          this.accountInfo.push({
            accId: this.accId,
            accName: this.accName,
            accAmount: this.accAmount
          })
        }
        else {
          return tool.warning('输入账户 金额')
        }
      }
      if(this.accId1) {
        this.accAmount1 = tool.xe.toNumber(this.accAmount1)
        if(this.accAmount1) {
          this.accountInfo.push({
            accId: this.accId1,
            accName: this.accName1,
            accAmount: this.accAmount1
          })
        }
        else {
          return tool.warning('输入账户 1 金额')
        }
      }
      if(this.accountInfo.length === 0) {
        return tool.warning('至少输入一个账户')
      }
      if(tool.xe.toNumber(this.currentRow.nowCheck) !== tool.add(this.accAmount, this.accAmount1)) {
        return tool.warning('收款合计金额不等于结余金额')
      }
      let total = tool.xe.add(this.accAmount, this.accAmount1)
      let rp = {
        buId: this.receiptList[0].buId,
        billDate: this.billDate,
        buName: this.receiptList[0].buName,
        accId: this.accId,
        doId: this.receiptList[0].doId,
        doName: this.receiptList[0].doName,
        accName: this.accName,
        accAmount: tool.xe.toNumber(this.accAmount,0),
        accId1: this.accId1,
        accName1: this.accName1,
        accAmount1: tool.xe.toNumber(this.accAmount1),
        totalAmount: total,
        amount: tool.xe.subtract(total, this.yhAmount),
        disAmount: tool.xe.toNumber(this.yhAmount),
        infos: this.receiptList
      }
      let list = tool.xe.clone(this.receiptList, true);
      if (list.length === 0) return tool.err('没有选择收款的票据');
      for (let i = 0; i < list.length; i++) {
        let left = tool.xe.toNumber(list[i].leftAmount);
        let hx = tool.add(list[i].nowCheck, list[i].yhAmount);
        let transType = list[i].transType;
        if (!hx) return tool.err(list[i].billNo + '行没有核销金额');
        if (transType === 150602) {
          if (-hx > -left) return tool.err(list[i].billNo +'核销金额大于结余金额');
        } else {
          if (hx > left) return tool.err(list[i].billNo +'核销金额大于结余金额');
        }
      }
      tool.post('receipt/donerp', rp).then((ret) => {
        if (ret.stat) {
          this.$emit('saveSuc')
          this.showAccount = false
        }
      })
    },
  }
}
</script>
<style scoped lang="scss">
.price {
  color: #c45656;
  font-size: 18px;
}
.yh {
  font-size: 16px;
  color: #656D78;
}

</style>